package com.zuikaku.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ResourceBundle;

@Component
public class AliyunSMS implements InitializingBean {


    private String accessKey;

    private String secretKey;

    private Logger logger = LoggerFactory.getLogger(AliyunSMS.class);
    public void send(String phoneNumbers, String paramJson, String templateId) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, secretKey);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "最咔酷线上教程");
        request.putQueryParameter("TemplateCode", templateId);
        request.putQueryParameter("TemplateParam", paramJson);
        logger.info("send sms request:{}", JSONObject.toJSONString(request));
        try {
            CommonResponse response = client.getCommonResponse(request);
            logger.info(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ResourceBundle resourceBUndle = ResourceBundle.getBundle("aliyunConfig");
        accessKey=resourceBUndle.getString("accessKey");
        secretKey=resourceBUndle.getString("secretKey");

    }
}
